import { parseTime } from '@/utils/ruoyi'

/**
 * 获取N周前 周开端日期
 * @param {*} weeksAgo 
 * @returns 
 */
export function getWeekStartDate (weeksAgo) { 
    // 先获取N周前的日期
    var today = new Date();
    var date = new Date(today.getTime() - (weeksAgo * 7 * 24 * 60 * 60 * 1000));
    //获得对应的开端日期
    let weekStartDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() -  date.getDay() + 1);
    return parseTime(weekStartDate);
}

/**
 * 获取N天前 00:00:00的日期数据
 * @param {*} daysAgo 
 * @returns 
 */
export function getDayStartDate (daysAgo) { 
    var today = new Date();
    var date = new Date(today.getTime() - (daysAgo * 24 * 60 * 60 * 1000));
    let startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
    return parseTime(startDate);
}

/**
 * 获取N个月前 1日 00:00:00的日期数据
 * @param {*} monthsAgo 
 * @returns 
 */
export function getMonthStartDate (monthsAgo) { 
    var today = new Date();
    var nowMonth = today.getMonth();
    if(nowMonth > monthsAgo) {
        // 还在今年范围内
        let startDate = new Date(today.getFullYear(), nowMonth - monthsAgo, 1);
        return parseTime(startDate);
    }
    // 如果不在今年范围内 看看跨了几年
    var years = Math.floor((monthsAgo - nowMonth) / 12) + 1;
    var nowMonth = nowMonth + (12 * years) - monthsAgo;
    let startDate = new Date(today.getFullYear()- years, nowMonth, 1);
    return parseTime(startDate);
}
